<?php

namespace ServiceSdk\Services;

use xiaoyaoSDK\Libs\Remote;

class DemoService extends BaseService
{
    private static $instance = null;

    /**
     * Description: 单例实例化
     * @return DemoService
     * @Author: Bililovy
     * @Date  : 19-6-10 下午9:56
     */
    public static function getInstance()
    {
        if (is_null(self::$instance) || self::$instance instanceof self) {
            self::$instance = new self();
        }

        return self::$instance;
    }


    // 业务示例，可以删除
    public function demo()
    {
        $response = Remote::post('alliance_us.remove_fetter_data', json_encode(['_secret_data' => $encryptData]));
        $result = $this->dealResponse($response);
        // 此SDK项目中所有的返回值必须经过以下方法统一处理，标准返回
        return $this->dealResponse($response);
    }

    /**
     * Description: 统一处理改服务下的返回值，内部结构需要根据实际情况，自行改动
     *
     * @param array $response
     *
     * @return array
     * @Author: Bililovy
     * @Date  : 19-6-10 下午9:48
     */
    protected function dealResponse($response): array
    {
        $this->setErrorCode($response[ 'errorCode' ] ?? 0);
        if (isset($response[ 'errorCode' ]) && $response[ 'errorCode' ] === self::SUCCESS_CODE) {
            $response = empty($response[ 'data' ]) ? ['success'] : $response[ 'data' ];
        } else {
            if (isset($response[ 'message' ])) {
                $this->setErrorMessage($response[ 'message' ]);
            } else if (is_string($response)) {
                $this->setErrorMessage($response);
            } else {
                $this->setErrorMessage('请求处理失败，请检查原因');
            }

            $response = [];
        }

        return $response;
    }

}
